import numpy

class SteinhartHart:
    def __init__(self):
        self.A = 0.0
        self.B = 0.0
        self.C = 0.0
        
        self.Temp1 = 0.0
        self.Temp2 = 25.0
        self.Temp3 = 50.0
        
        self.R1 = 16330
        self.R2 = 5000
        self.R3 = 1801
        
        self.Calibrate()
        
        
    def Calibrate(self):
        b = numpy.reshape(numpy.array((1.0/(self.Temp1 + 273.0), 1.0/(self.Temp2 + 273.0), 1.0/(self.Temp3 + 273.0))), (3,1))
        A=numpy.array(((1.0, numpy.log(self.R1), numpy.power(numpy.log(self.R1), 3)),
            (1.0, numpy.log(self.R2), numpy.power(numpy.log(self.R2), 3)),
            (1.0, numpy.log(self.R3), numpy.power(numpy.log(self.R3), 3))))
        AInv = numpy.linalg.inv(A)
        Res = numpy.matrix(AInv) * numpy.matrix(b)
        self.A = float(Res[0])
        self.B = float(Res[1])
        self.C = float(Res[2])
        
    def GetTemp(self, R):
        return 1.0/(self.A + self.B*numpy.log(R) + self.C * (numpy.power(numpy.log(R),3)))
        
    def SetCalibrationPoint(self, Temp, Res, Point):
        if Point == 0:
            self.Temp1 = Temp
            self.R1 = Res
        if Point == 1:
            self.Temp2 = Temp
            self.R2 = Res
        if Point == 2:
            self.Temp3 = Temp
            self.R3 = Res
    
    def GetCalPoints(self):
        return [(self.Temp1, self.R1), (self.Temp2, self.R2), (self.Temp3, self.R3)]
    
    def GetCoeffs(self):
        return [self.A, self.B, self.C]